
package labb3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TextClient {
    private String hand;
    private String name;
    private BufferedReader in;
    private PrintWriter ut;
    private String cmphand,greet;
    public TextClient(String nmn, String host, int port){
        name = nmn;
        try {
            Socket socket = new Socket(host,port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            ut = new PrintWriter(socket.getOutputStream());
            }
        catch(IOException e){
           System.err.println(e+". Det vill säga att den vägrar koppla upp sig.");
            }
        
        try {
            ut.println(name);
            ut.flush();
            greet = in.readLine();
        } 
        catch(IOException e){
        System.err.println(e);
        }
        }
    
        public String getGreet(String x){
            return greet;
        }
        

        
        public String compplay(String hnd){
            hand = hnd;
            //System.out.println(hand);
            //while (true){
                try{
                    ut.println(hand); 
                    ut.flush();
                    cmphand = in.readLine();
                }
                catch(IOException e){
                    System.err.println(e);
                }
                //if(cmphand==null || cmphand.equals("")){
                    
                //}
                           
            //}
            return cmphand;
            
        } 
        public String winner(){
            String winner = new String();
            if (cmphand != null){
                if (cmphand.equals(hand)){
                    winner = "draw";
                }
                else if ((cmphand.equals("SAX") && hand.equals("PASE")) || 
                        (cmphand.equals("STEN") && hand.equals("SAX")) || 
                        (cmphand.equals("PASE") && hand.equals("STEN"))){
                        winner = "cmp";
                }
                else{
                    winner = "player";
                }
            }
            return winner;
        }
}
          
